﻿myApp.controller('AgentRentController', function ($scope, $http, $routeParams, $location,$rootScope) {
    $scope.isAndroid = $rootScope.isAndroid;
    pageInit();

    $scope.sortOption = "Sorted by Name (A to Z)";

    var BASE_URL;
    if ($location.host() == "localhost") {
        BASE_URL = $location.protocol() + "://" + "localhost:" + $location.port();
    } else {
        BASE_URL = $location.protocol() + "://" + $location.host();
    }

    $scope.getShareURL = function (p) {
        //  return (BASE_URL + "/property/" + p.propertyid + "/S" + p.Zoning[0]);
        return (BASE_URL + "/property/" + p.propertyid + "/"  + p.SaleType);
    }

    $scope.getShareTitle = function (p) {
        return p.ShortHeading;
    }

    $scope.getShareDescription = function (p) {
        return p.LongHeading;
    }

    $scope.getShareImageURL = function (p) {
        return (p.PropertyURL || "https://c1.staticflickr.com/3/2280/32796837675_cd6b733c19_m.jpg");
    }

    $scope.getShareGoogleImageURL = function (p) {
        var url = (p.PropertyURL || "https://c1.staticflickr.com/3/2280/32796837675_cd6b733c19_m.jpg");
        return encodeURI(encodeURI(url));
    }


    $scope.paginate = function (value) {
        var begin, end, index;
        begin = ($scope.currentPage - 1) * $scope.numPerPage;
        end = begin + $scope.numPerPage;
        index = $scope.searchData.indexOf(value);
        return (begin <= index && index < end);
    };


    $scope.isPropertyTransactSale = function (obj) {
        return obj == 'S' ? true : false;
    }


    $scope.updateFilterOption = function (option, sortedChanged) {
        $scope.SortOptionChanged = sortedChanged;
        //$scope.$apply();
        console.log("HEY!" + option);
        switch (option) {
            case 0:
                $scope.sortOption = "Sorted by Name (A to Z)";
                $scope.sortKey = 'Project';
                break;
            case 1:
                $scope.sortOption = "Sorted by Name (Z to A)";
                $scope.sortKey = '-Project';
                break;
        
        }
    }

    $scope.isEmpty = function (obj) {
        for (var i in obj) if (obj.hasOwnProperty(i)) return false;
        return true;
    };


    function pageInit() {
        //var dummyData = [{ "propertyid": "e6ed6b22-91c9-43fe-89e8-049dac0a1fa6", "Latitude": 0.000000, "Longitude": 0.000000, "road": "RAJA UDA JALAN", "location": "RAJA UDA", "state": "PENANG MAINLAND", "type": "3/S S/HSE", "staffid": "d9899bc7-f9b5-4fca-a3b3-e75021ebac77", "agentname": "MONY NG", "mobile": "0124276737", "email": "monyng@lskworld.com", "StaffPicURL": "https://farm4.staticflickr.com/3872/18729593823_bb79eae979_m.jpg", "LandSqrtFt": 1400.00, "BuildUp": 1400.00, "Project": "WOLLEY", "ProjectRemark": null, "Developer": null, "PropertyDesc": null, "Room": 1, "Bathroom": 1, "FloorLoad": null, "Ceiling": null, "BestBuy": 0, "Tenure": "Freehold", "Facing": "", "Zoning": "Commercial", "Furnished": "Not Furnished", "FurnishedRemark": null, "Renovated": "Not Renovated", "RenovatedRemark": null, "Condition": "", "RRAmt": null, "RCAmt": 1300.00, "RIAmt": null, "RLAmt": null, "RentPrice": 1300.00, "UpdateDate": "2016-06-24T16:54:16.73", "CreateDate": "2014-01-28T11:38:29.72", "UpdatedDate": "2016-06-24T16:54:16.73", "PropertyURL": null, "LongHeading": "THREE-STOREY SHOPHOUSE, WOLLEY", "ShortHeading": "3/S S/HSE, WOLLEY", "Remark": null, "SaleType": "RC" }, { "propertyid": "5ba4cd9c-9a41-48a4-8351-08fd92094ec6", "Latitude": 0.000000, "Longitude": 0.000000, "road": "RAJA UDA JALAN", "location": "RAJA UDA", "state": "PENANG MAINLAND", "type": "3/S S/HSE", "staffid": "d9899bc7-f9b5-4fca-a3b3-e75021ebac77", "agentname": "MONY NG", "mobile": "0124276737", "email": "monyng@lskworld.com", "StaffPicURL": "https://farm4.staticflickr.com/3872/18729593823_bb79eae979_m.jpg", "LandSqrtFt": 1400.00, "BuildUp": 1400.00, "Project": "WOLLEY", "ProjectRemark": null, "Developer": null, "PropertyDesc": null, "Room": 1, "Bathroom": 1, "FloorLoad": null, "Ceiling": null, "BestBuy": 0, "Tenure": "Freehold", "Facing": "", "Zoning": "Commercial", "Furnished": "Not Furnished", "FurnishedRemark": null, "Renovated": "Not Renovated", "RenovatedRemark": null, "Condition": "", "RRAmt": null, "RCAmt": 3200.00, "RIAmt": null, "RLAmt": null, "RentPrice": 3200.00, "UpdateDate": "2016-06-24T16:52:56.78", "CreateDate": "2014-01-28T11:33:52.46", "UpdatedDate": "2016-06-24T16:52:56.78", "PropertyURL": null, "LongHeading": "THREE-STOREY SHOPHOUSE, WOLLEY", "ShortHeading": "3/S S/HSE, WOLLEY", "Remark": null, "SaleType": "RC" }, { "propertyid": "f5938046-b00a-4280-b991-4e2db3c3fa8f", "Latitude": 0.000000, "Longitude": 0.000000, "road": "TODAK 3 JALAN    ", "location": "SEBERANG JAYA", "state": "PENANG MAINLAND", "type": "3/S S/HSE", "staffid": "d9899bc7-f9b5-4fca-a3b3-e75021ebac77", "agentname": "MONY NG", "mobile": "0124276737", "email": "monyng@lskworld.com", "StaffPicURL": "https://farm4.staticflickr.com/3872/18729593823_bb79eae979_m.jpg", "LandSqrtFt": 1400.00, "BuildUp": 1400.00, "Project": "PUSAT BANDAR SUNWAY", "ProjectRemark": null, "Developer": null, "PropertyDesc": null, "Room": 0, "Bathroom": 0, "FloorLoad": null, "Ceiling": null, "BestBuy": 0, "Tenure": "Freehold", "Facing": "", "Zoning": "", "Furnished": "Not Furnished", "FurnishedRemark": null, "Renovated": "Not Renovated", "RenovatedRemark": null, "Condition": "", "RRAmt": null, "RCAmt": 4200.00, "RIAmt": null, "RLAmt": null, "RentPrice": 4200.00, "UpdateDate": null, "CreateDate": "2016-08-18T15:59:16.473", "UpdatedDate": "2016-08-18T15:59:16.473", "PropertyURL": null, "LongHeading": "THREE-STOREY SHOPHOUSE, PUSAT BANDAR SUNWAY", "ShortHeading": "3/S S/HSE, PUSAT BANDAR SUNWAY", "Remark": null, "SaleType": "RC" }, { "propertyid": "6a73126e-6c2a-4fe1-b379-7b60a8b26280", "Latitude": 0.000000, "Longitude": 0.000000, "road": "KAMPUNG BENGGALI JALAN    ", "location": "BUTTERWORTH", "state": "PENANG MAINLAND", "type": "APT", "staffid": "d9899bc7-f9b5-4fca-a3b3-e75021ebac77", "agentname": "MONY NG", "mobile": "0124276737", "email": "monyng@lskworld.com", "StaffPicURL": "https://farm4.staticflickr.com/3872/18729593823_bb79eae979_m.jpg", "LandSqrtFt": 780.00, "BuildUp": 780.00, "Project": "RIA APARTMENT", "ProjectRemark": null, "Developer": null, "PropertyDesc": null, "Room": 3, "Bathroom": 2, "FloorLoad": null, "Ceiling": null, "BestBuy": 0, "Tenure": "Freehold", "Facing": "N/A", "Zoning": "Residential", "Furnished": "Furnished", "FurnishedRemark": null, "Renovated": "Renovated", "RenovatedRemark": null, "Condition": "", "RRAmt": 680.00, "RCAmt": null, "RIAmt": null, "RLAmt": null, "RentPrice": 680.00, "UpdateDate": "2016-09-06T16:46:39.217", "CreateDate": "2016-09-05T17:46:07.613", "UpdatedDate": "2016-09-06T16:46:39.217", "PropertyURL": null, "LongHeading": "APARTMENT, RIA APARTMENT", "ShortHeading": "APT, RIA APARTMENT", "Remark": null, "SaleType": "RR" }, { "propertyid": "c4a31964-e958-4c6a-ae72-ca3fbd4939b3", "Latitude": 0.000000, "Longitude": 0.000000, "road": "CHAIN FERRY JALAN    ", "location": "BUTTERWORTH", "state": "PENANG MAINLAND", "type": "4/S S/HSE", "staffid": "d9899bc7-f9b5-4fca-a3b3-e75021ebac77", "agentname": "MONY NG", "mobile": "0124276737", "email": "monyng@lskworld.com", "StaffPicURL": "https://farm4.staticflickr.com/3872/18729593823_bb79eae979_m.jpg", "LandSqrtFt": 1400.00, "BuildUp": 1400.00, "Project": null, "ProjectRemark": null, "Developer": null, "PropertyDesc": null, "Room": 0, "Bathroom": 0, "FloorLoad": null, "Ceiling": null, "BestBuy": 0, "Tenure": "Freehold", "Facing": "", "Zoning": "Commercial", "Furnished": "Not Furnished", "FurnishedRemark": null, "Renovated": "Not Renovated", "RenovatedRemark": null, "Condition": "", "RRAmt": null, "RCAmt": 2000.00, "RIAmt": null, "RLAmt": null, "RentPrice": 2000.00, "UpdateDate": "2016-08-18T13:28:52.357", "CreateDate": "2011-07-13T11:20:55.89", "UpdatedDate": "2016-08-18T13:28:52.357", "PropertyURL": null, "LongHeading": "4-STOREY SHOPHOUSE", "ShortHeading": "4/S S/HSE", "Remark": null, "SaleType": "RC" }, { "propertyid": "46ed3630-3caf-4dfc-ae74-f15d603e7d66", "Latitude": 0.000000, "Longitude": 0.000000, "road": "RAJA UDA JALAN", "location": "RAJA UDA", "state": "PENANG MAINLAND", "type": "3/S S/HSE", "staffid": "d9899bc7-f9b5-4fca-a3b3-e75021ebac77", "agentname": "MONY NG", "mobile": "0124276737", "email": "monyng@lskworld.com", "StaffPicURL": "https://farm4.staticflickr.com/3872/18729593823_bb79eae979_m.jpg", "LandSqrtFt": 1400.00, "BuildUp": 4200.00, "Project": "WOLLEY", "ProjectRemark": null, "Developer": null, "PropertyDesc": null, "Room": 3, "Bathroom": 3, "FloorLoad": null, "Ceiling": null, "BestBuy": 0, "Tenure": "Freehold", "Facing": "", "Zoning": "Commercial", "Furnished": "Not Furnished", "FurnishedRemark": null, "Renovated": "Not Renovated", "RenovatedRemark": null, "Condition": "", "RRAmt": null, "RCAmt": 5500.00, "RIAmt": null, "RLAmt": null, "RentPrice": 5500.00, "UpdateDate": "2016-06-24T16:54:30.753", "CreateDate": "2014-01-28T11:28:36.357", "UpdatedDate": "2016-06-24T16:54:30.753", "PropertyURL": null, "LongHeading": "THREE-STOREY SHOPHOUSE, WOLLEY", "ShortHeading": "3/S S/HSE, WOLLEY", "Remark": null, "SaleType": "RC" }]
        //$scope.searchData = dummyData;

        //$scope.totalItems = $scope.searchData.length;
        //$scope.currentPage = 1;
        //$scope.numPerPage = 10;
        $scope.username = $routeParams.id;
        var url = 'api/Property/Agent_rent/' + $routeParams.id;
        $http.get(url).success(function (data) {
            $scope.searchData = data;
            console.log("DATA: " + JSON.stringify(data));
            $scope.totalItems = $scope.searchData.length;
            $scope.currentPage = 1;
            $scope.numPerPage = 10;
        })
    }

});